// pages/details/details.js

import { goodscontent,goodspj,addCart,isLike,addLike,delLike } from "../../utils/http";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        info:[],
        list:[],
        show:false,
        number:1,
        sku:[],
        indexss:0,
        likeShow:false
    },
    
    addLike(e){
      let {likeShow}=this.data
      console.log(e.currentTarget.dataset.id);
      // likeShow=!likeShow
      if(likeShow==false){
        addLike({goodsId:e.currentTarget.dataset.id,type:0,token:wx.getStorageSync('token')}).then(res=>{
          console.log(res)
          this.getContents(e.currentTarget.dataset.id)
          this.showLike(e.currentTarget.dataset.id)
        })
      }else{
        delLike({goodsId:e.currentTarget.dataset.id,type:0,token:wx.getStorageSync('token')}).then(res=>{
          console.log(res)
          this.showLike(e.currentTarget.dataset.id)
          this.getContents(e.currentTarget.dataset.id)
        })
      }
      this.setData({
        likeShow
      })
    },
    
    showLike(id){
      // console.log(id);
      let {likeShow} = this.data
      isLike({type:0,goodsId:id,token:wx.getStorageSync('token')}).then(res=>{
        console.log(res);
        if(res.code==0){
          likeShow=true
        }else{
          likeShow=false
        }
        this.setData({
          likeShow
        })
      })
    },
    addcarts(){
        let {number,info,sku}=this.data
        addCart({goodsId:info.basicInfo.id,number:number,sku:sku,token:wx.getStorageSync('token')}).then(res=>{
            console.log(res);
        })
    },
    onChange(e){
        let {number}=this.data
        console.log(e);
        number=e.detail
        this.setData({
            number
        })
    },
    onClickButton(e){
        let {show}=this.data
        show=!show
        this.setData({
            show
        })
    },
    getContents(id){
      let {info} = this.data
      goodscontent(id).then(res=>{
        console.log(res);
        info=res.data
        this.setData({
            info
        })
    })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      console.log(options);
          this.showLike(options)
        let {info,list,likeShow} = this.data
        
        this.getContents(options.id)

        goodspj(options.id).then(res=>{
            console.log(res);
            if(res.data){
                list=res.data
            }else{
                console.log('暂无数据');
            }
            this.setData({
                list
            })
        })

        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})